package com.itany.shop.service.proxy;

import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.tran.TransactionManager;
import com.itany.shop.vo.Cart;

/**
 * @Author:石小俊
 * @Date:2022/11/16 14:05
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
public class CartServiceProxy implements CartService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartServiceTarget");

    @Override
    public void addCart(Integer id, Cart cart) throws UserNotLoginException, ProductNotExistException {
        try {
            tran.begin();
            cartService.addCart(id,cart);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (ProductNotExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyNum(Integer id, Integer num, Cart cart) throws UserNotLoginException, ProductNotExistException {
        try {
            tran.begin();
            cartService.modifyNum(id,num,cart);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (ProductNotExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void removeCart(Integer id, Cart cart) throws UserNotLoginException, ProductNotExistException {
        try {
            tran.begin();
            cartService.removeCart(id,cart);
            tran.commit();
        } catch (UserNotLoginException e) {
            tran.rollback();
            throw e;
        } catch (ProductNotExistException e) {
            tran.rollback();
            throw e;
        }
    }
}
